<template>
  <el-drawer
      title="编辑组织"
      :visible.sync="visible"
      :before-close="handleClose"
      :wrapperClosable="false"
      :size="this.drawerSize"
  >
    <div class="drawer-content">
      <el-form ref="form" :model="form" :rules="rules"  label-width="120px">
        <el-form-item label="组织名称" prop="orgName">
          <el-input v-model="form.orgName"></el-input>
        </el-form-item>
        <el-form-item label="负责人姓名">
          <el-input v-model="form.personCharge"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.tel"></el-input>
        </el-form-item>
        <el-form-item label="是否作为栏目">
          <el-switch v-model="form.navFlag" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">修改</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </div>

  </el-drawer>
</template>

<script>
import { DRAWER_SIZE, PLEASE_INPUT } from "@/util/constant";
import { getOrgDetailApi, updateOrgApi } from "@/api/modules/org.api";
export default {
  data() {
    return {
      visible: false,
      form: {
        orgName: '',
        personCharge: '',
        tel: '',
        orgId: '',
        parentId: 0,
        navFlag: false
      },
      drawerSize: DRAWER_SIZE,
      rules: {
        orgName: [
          { required: true, message: PLEASE_INPUT, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    async init(id) {
      const res = await getOrgDetailApi(id)
      this.form.orgName = res.orgName
      this.form.personCharge = res.personCharge
      this.form.tel = res.tel
      this.form.orgId = res.orgId
      this.form.navFlag = res.navFlag
      this.visible = true
    },
    handleClose(){
      this.form = {}
      this.visible = false
    },
    async handleAdd(){
      this.form.parentId = 0
      this.$refs.form.validate(async (valid) => {
        if(valid){
          await updateOrgApi(this.form)
          this.$message({
            message: '成功',
            type: 'success'
          });
          this.form = {}
          this.visible = false
          this.$emit('getParentTableData')
        }
      })
    },
  }
}
</script>
